package com.seo.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.service.user.sdk.UserDomainService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * UserDomainController <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/user/domain")
public class UserDomainController {

	@Autowired
	private UserDomainService userDomainService;

	@RequestMapping
	public String index(HttpServletRequest request, HttpSession session) {
		log.info("进入用户域名列表...");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		ReturnBody rb = userDomainService.getUserDomain(userId);

		request.setAttribute("port", rb.get("port"));

		request.setAttribute("domain", rb.get("domain"));
		request.setAttribute("userDomainList", rb.get("userDomainList"));
		request.setAttribute("proxy", Jurisdiction.roleCode("proxy"));
		return "user/domain/list";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		String userName = Jurisdiction.getT("userName", String.class, ClientConst.SESSION_USER);
		log.info("用户名-[{}]", userName);
		String recordValue = SecureUtil.md5(userName);
		log.info("jinfaren-verify记录值-[{}]", recordValue);
		request.setAttribute("hostRecord", "jinfaren-verify");
		request.setAttribute("recordType", "TXT");
		request.setAttribute("recordValue", recordValue);

		return "user/domain/add";
	}

	@ResponseBody
	@RequestMapping("/verify")
	public ReturnBody verify(String domain) {
		return userDomainService.verify(domain);
	}

	@RequestMapping("/binding")
	public String binding(HttpServletRequest request, String hostRecord, String domain) {
		String recordValue = userDomainService.getDefaultDomain();
		log.info("[{}]记录值-[{}]", hostRecord, recordValue);
		request.setAttribute("hostRecord", hostRecord);
		request.setAttribute("recordType", "CNAME");
		request.setAttribute("recordValue", recordValue);
		request.setAttribute("domain", domain);

		return "user/domain/binding";
	}

	@ResponseBody
	@RequestMapping("/save")
	public ReturnBody save(String hostRecord, String domain) {
		return userDomainService.save(hostRecord, domain);
	}

	@ResponseBody
	@RequestMapping("/updateState")
	public ReturnBody updateState(String state, int id) {
		Map<String, Object> params = new HashMap<>();
		params.put("state", state);
		params.put("id", id);
		return userDomainService.updateUserDomain(params);
	}

	@ResponseBody
	@RequestMapping("/delete")
	public ReturnBody delete(int id) {
		return userDomainService.deleteUserDomain(id);
	}

}
